<cfcomponent>

	<cfscript>
		this.name = "CSAC_Publications";
		this.clientManagement = true;
		this.datasource = "CSAC";
		this.clientStorage = "CSAC";
		this.loginStorage = "cookie";
		this.applicationTimeout = CreateTimeSpan(100,0,0,0);
	</cfscript>
	
	<cffunction name="onApplicationStart">
		<cfscript>
			StructClear(application);
			
			application.mode = IIf(cgi.HTTP_HOST eq "csac" or cgi.HTTP_HOST eq "publications", DE("dev"), DE("prod"));
			var mappings = {};
			
			mappings.js = {};
			
				mappings.js.local = {};
				
					mappings.js.local.application = {};
						mappings.js.local.application = "/assets/csac/ui/common.js";
				
					mappings.js.local.jquery = {};
						mappings.js.local.jquery.all = "/assets/jquery/jquery.min.js";
						mappings.js.local.jquery.ui = "/assets/jquery/jquery-ui.min.js";
						mappings.js.local.jquery.mobile = "/assets/jquery/jquery-mobile.min.js";
						mappings.js.local.jquery.color = "/assets/jquery/jquery.color.min.js";
						
					mappings.js.local.kendo = {};
						mappings.js.local.kendo.all = "/assets/kendoui/js/kendo.all.min.js";
						mappings.js.local.kendo.web = "/assets/kendoui/kendo.web.min.js";
						
					mappings.js.local.bootstrap = {};
						mappings.js.local.bootstrap.all = "/assets/bootstrap/bootstrap.min.js";
						mappings.js.local.bootstrap.v3 = "/assets/bootstrap3/dist/js/bootstrap.min.js";
						mappings.js.local.bootstrap.typeahead = "/assets/bootstrap3/dist/js/typeahead.min.js";
						mappings.js.local.bootstrap.datepicker = "/assets/bootstrap.datepicker/js/bootstrap-datepicker.js";
						
					mappings.js.local.jquery.validator = {};
						mappings.js.local.jquery.validator.validator = "/assets/jquery.validator/dist/jquery.validate.min.js";
						mappings.js.local.jquery.validator.additional_methods = "/assets/jquery.validator/dist/additional-methods.min.js";
						
					mappings.js.local.misc = {};
						mappings.js.local.misc.holder = "/assets/misc/holder.js";
						mappings.js.local.misc.respond = "/assets/misc/respond.js";
						mappings.js.local.misc.ie10 = "/assets/misc/ie10.js";
						
				mappings.js.remote = {};
				
					mappings.js.remote.jquery = {};
						mappings.js.remote.jquery.all = "//code.jquery.com/jquery-1.10.2.min.js";
						mappings.js.remote.jquery.ui = "//code.jquery.com/ui/1.10.3/jquery-ui.min.js";
						mappings.js.remote.jquery.mobile = "//code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js";
						mappings.js.remote.jquery.color = "//code.jquery.com/color/jquery.color-2.1.2.min.js";
						
					mappings.js.remote.kendo = {};
						mappings.js.remote.kendo.all = "//da7xgjtj801h2.cloudfront.net/2013.1.319/js/kendo.all.min.js";
						mappings.js.remote.kendo.web = "//da7xgjtj801h2.cloudfront.net/2013.1.319/js/kendo.web.min.js";
						mappings.js.remote.kendo.dataviz = "//da7xgjtj801h2.cloudfront.net/2013.1.319/js/kendo.dataviz.min.js";
						mappings.js.remote.kendo.mobile = "//da7xgjtj801h2.cloudfront.net/2013.1.319/js/kendo.mobile.min.js";
						
					mappings.js.remote.bootstrap = {};
						mappings.js.remote.bootstrap.all = "//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js";
						mappings.js.remote.bootstrap.v3 = "//netdna.bootstrapcdn.com/bootstrap/3.0.0-rc1/js/bootstrap.min.js";
						
					mappings.js.remote.misc = {};
						mappings.js.remote.misc.webfont = "//ajax.googleapis.com/ajax/libs/webfont/1.4.8/webfont.js";
						mappings.js.remote.misc.swfobject = "//ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js";
					
			mappings.css = {};
					
				mappings.css.local = {};
				
					mappings.css.local.application = {};
						mappings.css.local.application.common = "/assets/csac/ui/common.css";
				
					mappings.css.local.jquery = {};
						mappings.css.local.jquery.ui = "/assets/jquery/jquery-ui.min.css";
						mappings.css.local.jquery.mobile = "/assets/jquery/jquery.mobile.min.css";
						mappings.css.local.jquery.qunit = "/assets/jquery/qunit.css";
						
					mappings.css.local.kendo = {};
						mappings.css.local.kendo.common = "/assets/kendoui/styles/kendo.common.min.css";
						mappings.css.local.kendo["default"] = "/assets/kendoui/styles/kendo.default.min.css";
						mappings.css.local.kendo.dataviz = "/assets/kendoui/styles/kendo.dataviz.min.css";
						mappings.css.local.kendo.datavizdefault = "/assets/kendo/styles/kendo.dataviz.default.min.css";
						
					mappings.css.local.bootstrap = {};
						mappings.css.local.bootstrap.all = "/assets/bootstrap/css/bootstrap-combined.min.css";
						mappings.css.local.bootstrap["no-icons"] = "/assets/bootstrap/css/bootstrap-combined.no-icons.min.css";
						mappings.css.local.bootstrap.v3 = "/assets/bootstrap3/dist/css/bootstrap.min.css";
						mappings.css.local.bootstrap.datepicker = "/assets/bootstrap.datepicker/css/datepicker.css";
						
					mappings.css.local.fontawesome = {};
						mappings.css.local.fontawesome.all = "/assets/font-awesome/css/font-awesome.min.css";
						mappings.css.local.fontawesome.ie7 = "/assets/font-awesome/css/font-awesome-ie7.min.css";
						
					mappings.css.local.fonts = {};
						mappings.css.remote.fonts.din = "/assets/fonts/din/din.css";
						
				mappings.css.remote = {};
				
					mappings.css.remote.jquery = {};
						mappings.css.remote.jquery.ui = "//code.jquery.com/ui/1.10.3/themes/ui-lightness/jquery-ui.min.css";
						mappings.css.remote.jquery.mobile = "//code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css";
						mappings.css.remote.jquery.qunit = "//code.jquery.com/qunit/qunit-1.12.0.css";
						
					mappings.css.remote.kendo = {};
						mappings.css.remote.kendo.common = "//da7xgjtj801h2.cloudfront.net/2013.1.319/styles/kendo.common.min.css";
						mappings.css.remote.kendo["default"] = "//da7xgjtj801h2.cloudfront.net/2013.1.319/styles/kendo.default.min.css";
						mappings.css.remote.kendo.dataviz = "//da7xgjtj801h2.cloudfront.net/2013.1.319/styles/kendo.dataviz.min.css";
						mappings.css.remote.kendo.datavizdefault = "//da7xgjtj801h2.cloudfront.net/2013.1.319/styles/kendo.dataviz.default.min.css";
						
					mappings.css.remote.bootstrap = {};
						mappings.css.remote.bootstrap.all = "//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css";
						mappings.css.remote.bootstrap["no-icons"] = "//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css";
						mappings.css.remote.bootstrap.v3 = "//netdna.bootstrapcdn.com/bootstrap/3.0.0-rc1/css/bootstrap.min.css";
						
					mappings.css.remote.fontawesome = {};
						mappings.css.remote.fontawesome.all = "//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css";
						mappings.css.remote.fontawesome.ie7 = "//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome-ie7.min.css";
					
					mappings.css.remote.fonts = {};
						mappings.css.remote.fonts.opensans = "//fonts.googleapis.com/css?family=Open+Sans:100,200,300,400,500,600,700,800,900";
						
			mappings.application = {};
			
				// paths are web paths e.g. /assets/foo
				mappings.application.paths = {};
				
					mappings.application.paths.root = "/";
					mappings.application.paths.com = "/assets/csac/com";
					mappings.application.paths.bos = "/assets/csac/com/bo";
					mappings.application.paths.gateways = "/assets/csac/com/gateway";
					mappings.application.paths.services = "/assets/csac/com/service";
					mappings.application.paths.ui = "/assets/csac/ui";
					mappings.application.paths.temp = "/assets/csac/temp";
				
				// directories are full directories including drive letter e.g. C:\inetpub\assets\foo
				mappings.application.directories = {};
				
					mappings.application.directories.root = ExpandPath(GetDirectoryFromPath(mappings.application.paths.root));
					mappings.application.directories.com = ExpandPath(GetDirectoryFromPath(mappings.application.paths.com));
					mappings.application.directories.bos = ExpandPath(GetDirectoryFromPath(mappings.application.paths.bos));
					mappings.application.directories.gateways = ExpandPath(GetDirectoryFromPath(mappings.application.paths.gateways));
					mappings.application.directories.services = ExpandPath(GetDirectoryFromPath(mappings.application.paths.services));
					mappings.application.directories.ui = ExpandPath(GetDirectoryFromPath(mappings.application.paths.ui));
					mappings.application.directories.temp = ExpandPath(GetDirectoryFromPath(mappings.application.paths.temp));
				
				// dotPaths are paths in dot notation
				mappings.application.dotPaths = {};
				
					mappings.application.dotPaths.root = "";
					mappings.application.dotPaths.com = "assets.csac.com";
					mappings.application.dotPaths.bos = "assets.csac.com.bo";
					mappings.application.dotPaths.gateways = "assets.csac.com.gateway";
					mappings.application.dotPaths.services = "assets.csac.com.service";
					mappings.application.dotPaths.ui = "assets.csac.ui";
					mappings.application.dotPaths.temp = "assets.csac.temp";
				
				// urls are root relative
				mappings.application.urls = {};
				
					mappings.application.urls.api = mappings.application.paths.com & "/API.cfc";
					mappings.application.urls.base = mappings.application.paths.com & "/Base.cfc";
					mappings.application.urls.controller = mappings.application.paths.ui & "/controller.cfm";
					
			application.mappings = mappings;
			// load base, api and the gateways into the application scope
			application.base = new assets.csac.com.Base();
			application.api = new assets.csac.com.API();
			application.gateway = new assets.csac.com.gateway.Gateway();
			application.orderGateway = new assets.csac.com.gateway.OrderGateway();
			application.orderProductGateway = new assets.csac.com.gateway.OrderProductGateway();
			application.productCategoryGateway = new assets.csac.com.gateway.ProductCategoryGateway();
			application.productGateway = new assets.csac.com.gateway.ProductGateway();
			application.trackingGateway = new assets.csac.com.gateway.TrackingGateway();
			application.userGateway = new assets.csac.com.gateway.UserGateway();
			application.pathToTemp = "/assets/csac/temp/";
			application.tempDirectory = ExpandPath(GetDirectoryFromPath(application.pathToTemp));
			application.administrator = {};
			application.administrator.name = "California Student Aid Commission";
			application.administrator.email = "support@csac.ca.gov";
			application.administrator.phone = "";
			application.developer = {};
			application.developer.name = "Syntric";
			application.developer.email = "support@syntric.com";
			application.developer.phone = "916-776-6650";
			application.fulfillment = {};
			application.fulfillment.name = "California Student Aid Commission";
			application.fulfillment.email = "csacpublications@csac.ca.gov";
			application.fulfillment.phone = "";
			application.notify = {};
			application.notify.newOrder = "ken@syntric.com";
			application.notify.orderShipped = "ken@syntric.com";
			application.notify.noteUpdate = "ken@syntric.com";
			application.notify.stockWarning = "ken@syntric.com";
			application.initDateTime = now();
			//initValidator();
		</cfscript>
	</cffunction>
	
	<cffunction name="onRequestStart">
	
		<!--- <cfif cgi.HTTPS eq "off">
			<cflocation url="https://#cgi.SERVER_NAME##cgi.SCRIPT_NAME#" addtoken="no">
		</cfif> --->
		
		<cfif IsDefined("initApp")>
			<cfscript>
				onApplicationStart();
			</cfscript>
		</cfif>
		
		<cflock scope="application" timeout="20" type="readonly">
			<cfset StructAppend(request, application) />
		</cflock>

		<cfset request.alerts = ArrayNew(1) />
		
		<cfif IsDefined("logout")>
			<cflogout />
		</cfif>
		
		<!--- cflogin is executed whenever the user is not logged in --->
		<cflogin>
			<cfif IsDefined("cflogin") and structKeyExists(cflogin, "name") and structKeyExists(cflogin, "password")>
				<cfinvoke component="assets.csac.com.gateway.UserGateway" method="query" returnvariable="authUser">
					<cfinvokeargument name="Email" value="#cflogin.name#" />
					<cfinvokeargument name="Password" value="#cflogin.password#" />
				</cfinvoke>
				<cfif authUser.RecordCount eq 1>
					<cfif authUser.Active eq 1>
						<cfloginuser name="#authUser.Email#" password="#authUser.Password#" roles="#authUser.Role#" />
					<cfelse>
						<cfset ArrayAppend(request.alerts, "Login failed.  Account found but not active.") />
					</cfif>
				<cfelseif authUser.RecordCount eq 0>
					<cfset ArrayAppend(request.alerts, "Login failed.  Account not found.") />
				<cfelseif authUser.RecordCount gt 1>
					<cfset ArrayAppend(request.alerts, "Login failed.  Duplicate accounts for #cflogin.name#.") />
				</cfif>
			</cfif>
		</cflogin>
		
		<cfif IsUserLoggedIn()>
			<cfinvoke component="assets.csac.com.gateway.UserGateway" method="query" returnvariable="validateUser">
				<cfinvokeargument name="Email" value="#GetAuthUser()#" />
				<cfinvokeargument name="Role" value="#GetUserRoles()#" />
				<cfinvokeargument name="Deleted" value="0" />
				<cfinvokeargument name="Active" value="1" />
			</cfinvoke>
			<cfif validateUser.RecordCount eq 1>
				<cfinvoke component="assets.csac.com.gateway.UserGateway" method="get" returnvariable="request.authUser">
					<cfinvokeargument name="User_ID" value="#validateUser.User_ID#" />
				</cfinvoke>
			<cfelse>
				<cflogout />
				<cflocation url="admin" addtoken="no" />
			</cfif>
		</cfif>

	<!--- <cfif IsDefined("initValidator")>
		<cfscript>
			initValidator();
		</cfscript>
	</cfif>  --->

	</cffunction>
	
	<cffunction name="onError">
		<cfargument name="Exception" required="true" />
		<cfargument name="EventName" required="true" />
		<cfif application.mode eq "prod">
			<cfmail to="kenhobbs@gmail.com" from="kenhobbs@gmail.com" subject="CSAC Publications Error" type="html">
				<cfdump var="#arguments.Exception#" expand="yes" label="Exception" />
				<cfdump var="#cgi#" expand="yes" label="CGI" />
				<cfdump var="#form#" expand="yes" label="Form" />
				<cfdump var="#url#" expand="yes" label="URL" />
				<cfdump var="#request#" expand="yes" label="request" />
			</cfmail>
			<h1>Ooops</h1>
			<p>The application has encountered an error.  An error message has been sent to the developer.</p>
			<p>If you require immediate assistance with this error, please <a href="mailto:support@syntric.com">contact the developer</a>.</p>
		<cfelse>
			<cfdump var="#arguments.Exception#" expand="no" label="Exception" />
			<cfdump var="#cgi#" expand="no" label="CGI" />
			<cfdump var="#form#" expand="no" label="Form" />
			<cfdump var="#url#" expand="no" label="URL" />
			<cfdump var="#request#" expand="no" label="request" />
		</cfif>
		<cfabort>
	</cffunction>
	
	<cffunction name="onRequestEnd">
		<cfsetting showdebugoutput="no">
	</cffunction>

<!--- <cffunction name="initValidator">
<cfscript>
	lock scope="application" timeout="20" type="readonly"
	{
		var validatorConfig = {extraRuleValidatorComponentPaths = "/assets/csac/com/validators/", definitionPath = "/assets/csac/com/bo/"};
		application.validator = new validateThis.ValidateThis(validatorConfig);
	}
</cfscript>
</cffunction> --->

</cfcomponent>